const WXAPI = require('apifm-wxapi')

Page({
  onLoad: function(options) {
    console.log("获取库存列表")
    this.getStockList();
  },

  onShow: function(){
    // 进入界面刷新数据
    this.getStockList();
  },
  
  // 跳转到新增库存界面
  stockIn: function () {
    wx.navigateTo({
      url: "/pages/seller/stock-in/index"
    })
  },

  // 申请出库
  async stockOut(e) {
    var stockId = e.currentTarget.dataset.id;
    var fruitName = e.currentTarget.dataset.name;
    console.log("申请出库，库存Id："+stockId+" - "+fruitName)
    wx.navigateTo({
      url: "/pages/seller/stock-out/index?stockId="+stockId+"&fruitName="+fruitName
    })
  },

  /**
   * 获取库存信息
   */
  async getStockList() {
    console.log('getting orderList')
    const res = await WXAPI.getStockList(
      {
        "userId":wx.getStorageSync('userid'),
      }
    );
    console.log("库存获取结果："+JSON.stringify(res))
    if(res.code == 20000){
      this.setData({
        stockList: res.data,
        userType : wx.getStorageSync('usertype')
      });
    }
  },

  // 申请代卖
  async sellRequest(e) {
    var that = this;
    var stockId = e.currentTarget.dataset.id;
    var fruitName = e.currentTarget.dataset.name;
    console.log("申请代卖，库存Id："+stockId+"水果："+fruitName)
    wx.navigateTo({
      url: "/pages/seller/sell-request/index?stockId="+stockId+"&fruitName="+fruitName
    })
  },

  // 获取合同
  async getContract(e){
    var stockId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/seller/contract/index?stockId="+stockId
    })
  }
})